#include<vector>
#include<set>
using namespace std;

class ExamRoom {
private:
    set<int>seats;
    int N;
public:
    ExamRoom(int N):N(N) {

    }

    int seat() {
        if (seats.size() == 0) {
            seats.insert(0);
            return 0;
        }
        int dist = *seats.begin();
        int index = 0;
        int prevSeat = -1;
        for (auto it = seats.begin(); it != seats.end(); ++it) {
            if (prevSeat != -1) {
                int tmpDist = (*it - prevSeat) / 2;
                if (tmpDist > dist) {
                    dist = tmpDist;
                    index = prevSeat+tmpDist;
                }
            }
            prevSeat = *it;
        }
        if (seats.find(N-1)==seats.end()&&dist < (N - prevSeat - 1))
            index = N - 1;
        seats.insert(index);
        return index;
    }

    void leave(int p) {
        seats.erase(p);
    }
};

/**
 * Your ExamRoom object will be instantiated and called as such:
 * ExamRoom* obj = new ExamRoom(N);
 * int param_1 = obj->seat();
 * obj->leave(p);
 */